﻿<%@ WebHandler Language="C#" Class="addRegCourseDataFrame" %>

using System;
using System.Web;
using System.Web.SessionState;
using DatabaseComponent;
using System.Collections.Generic;
using System.Linq;

public class addRegCourseDataFrame : IHttpHandler, IRequiresSessionState
{
    HttpSessionState Session = null;
    HttpRequest Request = null;
    HttpResponse Response = null;
    
    public void ProcessRequest (HttpContext context) {

        Session = context.Session;
        Request = context.Request;
        Response = context.Response;


        start();
        
        
        
        
    }


    /// <summary>
    /// this function will getting data from request and save the tick to the database.
    /// </summary>
    protected void start()
    {
        try
        {
            long CoursesID = long.Parse(Request["CoursesID"]);

            string CurrentData = Request["CurrentData"];

            if (CurrentData.IndexOf("CourseFiles") >= 0)
            {
                CurrentData = "./" + CurrentData.Substring(CurrentData.IndexOf("CourseFiles"));
                
            }
            else
            {
                CurrentData = "";
            }

           

            long TotalSpendTime = long.Parse (Request["TotalSpendTime"]);
            

            Persons currentPersons = getCurrentPersons();

            if (currentPersons == null)
                throw new Exception();

            Courses objCourses = new Courses ();
            objCourses.ID = CoursesID;
            
            List<RegisteredCourses> lstRegistered = new List<RegisteredCourses>(from obj in currentPersons.lstRegisteredCourses where obj.objCourses.Equals(objCourses) select obj);

            if (lstRegistered.Count == 0)
                throw new Exception();


            if (lstRegistered[0].TotalSpendTime <= 0)
            {
                lstRegistered[0].LastTickDate = DateTime.Now.AddSeconds(-30);

                lstRegistered[0].TotalSpendTime = 0;
                lstRegistered[0].NoOfTicks = 0;
                
            
            }
            
            
            
            lstRegistered[0].CurrentData = CurrentData;
            
            
            //lstRegistered[0].TotalSpendTime = lstRegistered[0].TotalSpendTime + (long)((DateTime.Now - lstRegistered[0].LastTickDate.Value).TotalSeconds);


            int currentTime = 0;

            if (((TimeSpan)(DateTime.Now - lstRegistered[0].LastTickDate)).Seconds > 60)
                currentTime = 30;
            else
                currentTime = ((TimeSpan)(DateTime.Now - lstRegistered[0].LastTickDate)).Seconds;
            
                
            
            
            lstRegistered[0].LastTickDate = DateTime.Now;
            
            
            lstRegistered[0].NoOfTicks ++;



            lstRegistered[0].TotalSpendTime = lstRegistered[0].TotalSpendTime + currentTime;



            int noOfFinished = (int)(lstRegistered[0].TotalSpendTime / lstRegistered[0].objCourses.Duration);

            UserLogsDB objUsersLogsDB = new UserLogsDB(ConnectionStrings.LMS);

            List<UserLogs> lstUsersLogs = objUsersLogsDB.getUserLogsByRegisteredCoursesID(lstRegistered[0].ID);
            lstUsersLogs = new List<UserLogs>(from obj in lstUsersLogs where obj.Status == 2 select obj);


            if (noOfFinished > lstUsersLogs.Count)
            {

                GlobalVars.addLog(lstRegistered[0].ID, 2);
                lstRegistered[0].CourseStatus = (long)CourseEnum.Waiting_exam;

                if (lstRegistered[0].objCourses.lstQuestions.Count == 0)
                {
                    lstRegistered[0].CourseStatus = (long)CourseEnum.Completed;

                    if (GlobalVars.getAllSucceedFinishedExam(lstRegistered[0].ID).Count == 0)
                    {

                        GlobalVars.addLog(lstRegistered[0].ID, 4);
                        GlobalVars.sendLogMail(lstRegistered[0], currentPersons, 4);
                        GlobalVars.addFinishedExam(lstRegistered[0].ID, "", "", 100.0);

                    }



                }

            }
            else
            {
                if (!(lstRegistered[0].CourseStatus == (long)CourseEnum.Waiting_exam))
                {

                    lstRegistered[0].CourseStatus = (long)CourseEnum.Learning;
                }
            }


           
                

           


            RegisteredCoursesDB objRegisteredCourses = new RegisteredCoursesDB(ConnectionStrings.LMS);
            objRegisteredCourses.Update(lstRegistered[0]);

            Response.Write("true");
            
            
            
        }
        catch
        {
            Response.Write("false");
        }
    }


    /// <summary>
    /// this function will get the current session person.
    /// </summary>
    /// <returns>the person running the course (null for a hacker trying to see course)</returns>
    protected Persons getCurrentPersons()
    {
        HttpSessionState st = Session;
        if (st == null)
            return null;
        if (st["CurrentPerson"] == null)
        {
           
            return null;
        }
        Persons currentPerson = null;

        try
        {
            currentPerson = (Persons)st["CurrentPerson"];

            PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
            List<Persons> lstPersons = objPersonsDB.getPersonsByGenID(currentPerson.GenID);

            if (lstPersons.Count == 0)
            {
                return null;

            }

            currentPerson = lstPersons[0];

        }
        catch
        {
            return null;
        }






        return currentPerson;
    }
    
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}